﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Security.Cryptography;
using AuthorizeNet;
using System.Xml;
using System.IO;
using System.Data;
using System.Net;


public partial class Brainetics_Brainetics_ManualSubmitDataPak : System.Web.UI.Page
{

    
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnFInd_Click(object sender, EventArgs e)
    {
        SqlConnection SQLConn = new SqlConnection();
        SqlConnection ExtSQLConn = new SqlConnection();
        System.Data.DataTable dtCallData = new System.Data.DataTable("CallData");
        string sEnterpriseListID = txtContact_ID.Text;
        string sCampaignName = "Brainetics";
        string sServer = "COGSQL";
        string sProject = "Brainetics";

        int errNum = 0;
        string errDesc = "";

        SQLConn.ConnectionString = "Data Source=" + sServer + "; Initial Catalog=" + sProject + "; User ID = portaluser; Password = sqluser";
        SQLConn.Open();
        if (SQLConn.State == System.Data.ConnectionState.Open)
        {

            //Fill dtCallData Table
            //string SQLCallData = "Select replace(replace(replace(xmlorder, '<Username>ailAPIo</Username>', '<Username>johndoe</Username>'), '<Password>083iwkh</Password>', '<Password>x423423</Password>'),'<Quantity>0</Quantity>', '<Quantity>1</Quantity>')  as xmlOrder from " + sCampaignName + "_CallData (nolock) where enterpriselistid = " + sEnterpriseListID + " ";
            string SQLCallData = "Select xmlOrder from " + sCampaignName + "_CallData (nolock) where enterpriselistid = " + sEnterpriseListID + " ";
            //string SQLCallData = "Select replace(xmlOrder, '<Username>ailAPl0</Username>', '<Username>ailAPIo</Username>' )as xmlOrder from " + sCampaignName + "_CallData (nolock) where enterpriselistid = " + sEnterpriseListID + " ";
            
            SqlCommand SqlCmdCallData = new SqlCommand(SQLCallData, SQLConn);
            SqlCmdCallData.CommandType = System.Data.CommandType.Text;
            SqlDataAdapter dataAdapterCallData = new SqlDataAdapter(SqlCmdCallData);
            dataAdapterCallData.Fill(dtCallData);
            SQLConn.Close();

            //txtXML.Text = dtCallData.Rows[0]["xmlOrder"].ToString();
            XmlDocument xmlRequest = new XmlDocument();
            xmlRequest.LoadXml(dtCallData.Rows[0]["xmlOrder"].ToString());
   
            //txtResponse.Text = PostRequest("HTTPS://webservices.datapakservices.com/OrderServiceV1", txtXML.Text.Trim(), ref errNum, ref errDesc);     //production
            //string response = PostRequest("HTTPS://webservices.datapakservices.com/ForwardITGatewayTest", xmlRequest.OuterXml, ref errNum, ref errDesc); //testing
            txtResponse.Text = "Order # " + sEnterpriseListID + " " + PostRequest("HTTPS://webservices.datapakservices.com/OrderServiceV1", xmlRequest.OuterXml, ref errNum, ref errDesc);     //production

            

        }       
    }

    public string PostRequest(string url, string data, ref int errorNumber, ref string errorMessage)
    {
        // Create the Web Request Object 
        WebRequest request = WebRequest.Create(url);
        request.Method = "POST";
        request.Timeout = 60000;
        request.ContentType = "text/xml; charset=utf-8";

        byte[] bytes = System.Text.Encoding.UTF8.GetBytes(data);
        request.ContentLength = bytes.Length;

        Stream outputStream = request.GetRequestStream();
        outputStream.Write(bytes, 0, bytes.Length);
        outputStream.Close();

        WebResponse response = null;
        try
        {
            response = request.GetResponse();
            errorNumber = 0;
        }
        catch (System.Net.WebException e)
        {
            if (e.Status == WebExceptionStatus.ProtocolError)
            {
                errorNumber = (int)((HttpWebResponse)e.Response).StatusCode;
                errorMessage = ((HttpWebResponse)e.Response).StatusDescription;
                //get the response anyway
                response = e.Response;
            }
            else
            {
                errorNumber = 2;
                errorMessage = e.Message;
            }
        }
        catch (Exception e)
        {
            errorNumber = 1;
            errorMessage = e.Message;
        }

        if (response != null)
        {
            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);
            return reader.ReadToEnd();
        }
        else
        {
            return "";
        }
    }


    protected void bntClear_Click(object sender, EventArgs e)
    {
        txtResponse.Text= "";
    }
}

